package com.mreko.dashupload;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

//ServerCommunicateUtil for get response from server using httpclient
public  class ServerCommunicateUtil {
	private HttpClient httpClient = new DefaultHttpClient();
	private HttpGet httpGet;
	private HttpPost httpPost;
	private HttpResponse httpResponse;
	private JSONObject response;

	// get video status whether the video is upload completely or already exist
	public int getVideostatus(int videoId,int segmentTotal) {
		String functionName = "getVediostatus";
		int status=DashMessage.UPLOAD_NOTFOUND;
		int segmentReceived=0;
		httpPost=new HttpPost(DashMessage.SERVER_BASEURL+DashMessage.SERVER_GETVIDEOSTATUS);
		try {
			List<NameValuePair> postParams = new ArrayList<NameValuePair>();
			postParams.add(new BasicNameValuePair("videoId", videoId + ""));
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams,
					HTTP.UTF_8);
			httpPost.setEntity(entity);

			httpResponse = httpClient.execute(httpPost);
			HttpEntity responseEntity = httpResponse.getEntity();
			if (responseEntity != null) {

				response = new JSONObject(
						EntityUtils.toString(responseEntity));
				segmentReceived=response.getInt("response");
			}
			
		} catch (ClientProtocolException e) {
			Log.e(functionName,
					"ClientProtocolException exception: " + e.getMessage());
		} catch (IOException e) {
			Log.e(functionName, "IOException exception: " + e.getMessage());
		} catch (ParseException e) {
			Log.e(functionName, "ParseException exception: " + e.getMessage());
		} catch (JSONException e) {
			Log.e(functionName, "JSONException exception: " + e.getMessage());
		}
		
		if(segmentReceived==segmentTotal){
			status=DashMessage.UPLOAD_FINISHED;
		}else if(segmentReceived<segmentTotal&&segmentReceived>0){
			status=DashMessage.UPLOAD_UNFINISHED;
		};
		return status;
	};
	//use userName and video‘s title，segament to query video’s status
	public int getVideostatus(String title,int segmentTotal) {
		String functionName = "getVediostatus";
		int status=DashMessage.UPLOAD_NOTFOUND;
		int segmentReceived=0;
		httpPost=new HttpPost(DashMessage.SERVER_BASEURL+DashMessage.SERVER_GETVIDEOSTATUSBYUSERIDANDTITLE);
		try {
			List<NameValuePair> postParams = new ArrayList<NameValuePair>();
			postParams.add(new BasicNameValuePair("userId", DashMessage.USERID+""));
			postParams.add(new BasicNameValuePair("title", title));
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams,
					HTTP.UTF_8);
			httpPost.setEntity(entity);

			httpResponse = httpClient.execute(httpPost);
			HttpEntity responseEntity = httpResponse.getEntity();
			if (responseEntity != null) {

				response = new JSONObject(
						EntityUtils.toString(responseEntity));
				segmentReceived=response.getInt("response");
			}
			
		} catch (ClientProtocolException e) {
			Log.e(functionName,
					"ClientProtocolException exception: " + e.getMessage());
		} catch (IOException e) {
			Log.e(functionName, "IOException exception: " + e.getMessage());
		} catch (ParseException e) {
			Log.e(functionName, "ParseException exception: " + e.getMessage());
		} catch (JSONException e) {
			Log.e(functionName, "JSONException exception: " + e.getMessage());
		}
		
		if(segmentReceived==segmentTotal){
			//already exist and finished
			status=DashMessage.UPLOAD_FINISHED;
		}else if(segmentReceived<segmentTotal&&segmentReceived>0){
			//unfinished return how many segmentReceived
			status=segmentReceived;
		};
		//not found new upload
		return status;
	};
}
